package com.dream.service.impl;

import java.text.SimpleDateFormat; 
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.TimeAxisMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.TimeAxis;
import com.dream.pojo.TimeAxisExample;
import com.dream.service.able.All_Service_able;

/*
 * 点点滴滴页面的业务类
 */
@Service("Time_axis_Serviceimpl")
public class Time_Axis_Serviceimpl implements All_Service_able<TimeAxis>{

	
	
	/*
	 * 时光轴记录表的DAO代理对象
	 */
	@Resource(name="timeAxisMapper")
	private TimeAxisMapper time_axis_mapper;
	
	
	/*
	 * (查询所有有效的时间轴记录)
	 * @see com.dream.serviceable.Time_axis_Serviceable#listTime_axis()
	 */
	@Override
	public List <TimeAxis> list() {
		//切换数据源，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		TimeAxisExample time_axis_example=new TimeAxisExample();
		//设置排序规则，按年月日降序
		time_axis_example.setOrderByClause("year desc,mon desc,day desc,time desc");
		//创建查询条件工具内部类
		TimeAxisExample.Criteria criteria=time_axis_example.createCriteria();
		//设置条件,state列等于1的数据
		criteria.andStateEqualTo(1);
		//返回查询到的数据
		return time_axis_mapper.selectByExample(time_axis_example);
	}

	/*
	 * (添加时光记录)
	 * @see com.dream.serviceable.Time_axis_Serviceable#insert_time(java.lang.String)
	 */
	@Override
	public TimeAxis insert(String ... str) {
		//切换数据源，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建时间对象
		Date now = new Date();
		//创建格式化对象
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		//拿到当前时间的字符串
		String now_time=ft.format(now);
		//创建时光记录对象
		TimeAxis time_axis = new TimeAxis();
		//插入内容
		time_axis.setContext(str[0]);
		//插入年
		time_axis.setYear(Integer.parseInt(now_time.substring(0, 4)));
		//插入月
		time_axis.setMon(Integer.parseInt(now_time.substring(5, 7)));
		//插入日
		time_axis.setDay(Integer.parseInt(now_time.substring(8,10)));
		//插入状态
		time_axis.setState(1);
		//插入时分秒
		time_axis.setTime(new Date());
		//写入数据库
		return time_axis_mapper.insertSelective(time_axis)>0?time_axis:null;
	}

	@Override
	public TimeAxis getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}
}
